using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XSMT
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		//private SMS_LIST WML;
		ArrayList arr = new ArrayList();

		public XSMT(SMS_MO MO)
		{
			WMO = MO;
		}

		public XSMT()
		{
		}
		#endregion
		#region Method
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });
			string[] strReturnMsg = GetReturnMsg(msg);
			for(int j=0;j<strReturnMsg.Length;j++)
			{
				if(strReturnMsg[j]!="")
				{
					WMT.AddMessage(WMO.UserID, strReturnMsg[j], SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				}				
			}
			WMT.Insert(); 
			return;

		}        
		private bool ValidInput(string[] tmp)
		{
			if (tmp.Length <= 0 || tmp.Length > 2) return false;
			return true;
		}
		private bool checkIsNumber(string txNumber)
		{
			bool textIsNumeric = true;
			try
			{
				int.Parse(txNumber.Trim());
			}
			catch
			{
				textIsNumeric = false;
			}
			return textIsNumeric;
		}
		private string[] GetReturnMsg(string[] arr)
		{	
			string connString = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			SqlConnection oledbConn = new SqlConnection(connString);
			string strSQl ="begin";
			strSQl +=" declare @d  datetime";
			strSQl +=" set @d = (select max(ngay) from XOSO where Mamien='MT')";
			strSQl +=" select * from XOSO where Mamien='MT' and  convert(nvarchar(10),ngay,103) = convert(nvarchar(10),@d,103)";
			strSQl +=" end";
			string[] msgBody = new string[]{"","","","","","",""};			
			DataSet ds = new DataSet();						
			oledbConn.Open();
			//SqlCommand cmd = new SqlCommand(strSQl,oledbConn);			
			//oleda.SelectCommand = cmd;						
			SqlDataAdapter oleda = new SqlDataAdapter(strSQl,oledbConn);
			oleda.Fill(ds,"gets");
			oledbConn.Close();
			if(WMO.ServiceID.Replace("04","")=="6072")
			{	
				//Xem toan bo ket qua xoso mien trung moi nhat				
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP");	
			}				
			else if(WMO.ServiceID.Replace("04","")=="6172")					
			{	
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP");	
			}	
			else if(WMO.ServiceID.Replace("04","")=="6272")
			{
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP");							
			}
			else if(WMO.ServiceID.Replace("04","")=="6372")
			{
				//Xem toan bo ket qua xoso mien trung moi nhat
				int totalRecord = ds.Tables["gets"].Rows.Count;
				DateTime d;
				if(totalRecord>0)
				{
					for(int i=0;i<totalRecord;i++)
					{
						d = DateTime.Parse(ds.Tables["gets"].Rows[i]["ngay"].ToString());
						msgBody[i] = string.Concat(ds.Tables["gets"].Rows[i]["Matinh"].ToString().Trim(),string.Concat(d.Day,"/",d.Month),"\n",
							"DB:",ds.Tables["gets"].Rows[i]["GiaiDacBiet"].ToString().Trim().Replace(" ",""),"\n",
							"1:",ds.Tables["gets"].Rows[i]["GiaiNhat"].ToString().Trim().Replace(" ",""),"\n",
							"2:",ds.Tables["gets"].Rows[i]["GiaiNhi"].ToString().Trim().Replace(" ",""),"\n",
							"3:",ds.Tables["gets"].Rows[i]["GiaiBa"].ToString().Trim().Replace(" ",""),"\n",
							"4:",ds.Tables["gets"].Rows[i]["GiaiTu"].ToString().Trim().Replace(" ",""),"\n",
							"5:",ds.Tables["gets"].Rows[i]["GiaiNam"].ToString().Trim().Replace(" ",""),"\n",
							"6:",ds.Tables["gets"].Rows[i]["GiaiSau"].ToString().Trim().Replace(" ",""),"\n",
							"7:",ds.Tables["gets"].Rows[i]["GiaiBay"].ToString().Trim().Replace(" ",""));
					}
				}
				ds.Dispose();
			}
			else if(WMO.ServiceID.Replace("04","")=="6572")																							
			{	
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP");								
			}
			else if(WMO.ServiceID.Replace("04","")=="6772")				
			{																					
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP");									
			}
			else
			{
				msgBody[0] = ConfigSettings.ReadSetting("SMS_HELP_MT");
			}			
			
			return msgBody;			
		}
		#endregion
	}
}

